package com.feri.fs.provider.core;


import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.Date;

public class AliOssUtils {
    public static final String ALI_KEY="LTAI5t8QA19CRR2V15oHyHW8";
    public static final String ALI_KEYSECRET="OqUKmRFUnAYm0frzUc43uKnk5BNoaP";
    public static final String ENDPOINT = "oss-cn-beijing.aliyuncs.com";
    public static final String bucketName = "xphdata";
    private static OSS client;
    static {
       client=new OSSClientBuilder().build(ENDPOINT, ALI_KEY, ALI_KEYSECRET);
    }
    /**
     * 上传内容*/
    public static String upload(String objName,String content,Date edate) throws UnsupportedEncodingException {
        //实现内容上传
        client.putObject(bucketName, objName, new ByteArrayInputStream(content.getBytes("UTF-8")));
        //生成访问地址
        return createUrl(objName,edate);
    }
    /**
     * 上传文件*/
    public static String upload(String objName,byte[] data,Date edate){
        //实现内容上传
        client.putObject(bucketName, objName, new ByteArrayInputStream(data));
        //生成访问地址
        return createUrl(objName, edate);
    }
    /**
     * 上传文件*/
    public static String upload(String objName, InputStream data,Date edate){
        //实现内容上传
        client.putObject(bucketName, objName, data);
        //生成访问地址
        return createUrl(objName, edate);
    }
    /*生成访问路径*/
    public static String createUrl(String objName, Date date){
        return client.generatePresignedUrl(bucketName,objName,date).toString();
    }
    /**
     * 删除文件*/
    public static boolean delObject(String objName){
        try{
            client.deleteObject(bucketName,objName);
            return true;
        }catch (Exception e){
            return false;
        }
    }
}
